package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.annotation.AuthCheck;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.constant.UserConstant;
import com.yupi.springbootinit.mapper.SysteminfoMapper;
import com.yupi.springbootinit.model.entity.Systeminfo;
import com.yupi.springbootinit.service.SysteminfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/systeminfo")
@Slf4j
public class SystemInfoController {

    @Resource
    private SysteminfoMapper systeminfoMapper;

    @GetMapping("/getlastedinfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<String> getLastedInfo() {
        QueryWrapper<Systeminfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createTime");
        queryWrapper.last("limit 1");
        Systeminfo systeminfo = systeminfoMapper.selectOne(queryWrapper);
        Integer enrollment = systeminfo.getEnrollment();
        Integer sendSmsNum = systeminfo.getSendSmsNum();
        Date createTime = systeminfo.getCreateTime();
        return ResultUtils.success("注册总人数为:"+enrollment+"\t"+"已发送短信数为:"+sendSmsNum+"\t"+"最后更新时间为:"+ createTime);
    }

    @GetMapping("/getallinfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<Systeminfo>> getAllInfo() {
        QueryWrapper<Systeminfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createTime");
        return ResultUtils.success(systeminfoMapper.selectList(queryWrapper));
    }

    @Resource
    private SysteminfoService systeminfoService;

    /**
     * 删除三个月前的数据
     */
    @GetMapping("/delinfo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<String> delInfo() {
       systeminfoService.deleteOldInfo();
        return ResultUtils.success("删除三个月前的数据成功");
    }

}
